
package GUILag;

    import ControllerLag.LaneErrorCTR;
    import ControllerLag.EmployeeCTR;
    import ControllerLag.LaneCTR;
    import ModelLag.Employee;
    import ModelLag.Lane;
    import ModelLag.LaneError;
    import ModelLag.LaneErrorDescription;
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Locale;
    import javax.swing.JOptionPane;
    //import javax.swing.JOptionPane;


    
    
public class CreateLaneErrorGUI extends javax.swing.JFrame {
    private Date dateInTable;
    private String weekDay;
    private LaneErrorCTR laneErrCTR;
    private EmployeeCTR empCTR;
    private LaneCTR laneCTR;
    private LaneErrorGUI LCP;
     
    
    /** Creates new form CreateLaneErrorGUI */
    public CreateLaneErrorGUI() {
        initComponents();
        
        empCTR = new EmployeeCTR();
        laneCTR = new LaneCTR();
        laneErrCTR = new LaneErrorCTR();
        populateComboBoxes();
    }
    
    public CreateLaneErrorGUI(LaneErrorGUI LCP)
    {
        initComponents();
        empCTR = new EmployeeCTR();
        laneCTR = new LaneCTR();
        laneErrCTR = new LaneErrorCTR();
        this.LCP = LCP;
        this.dateInTable = LCP.getDateInTable();
        this.weekDay = LCP.getWeekDay();
        
        
        DateFormat dF = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.GERMANY);
        String date = dF.format(dateInTable).replace('.','-');
        
        datejLabel2.setText(date);
        weekdayjLabel1.setText(weekDay);
        errorCodejComboBox1.addItem("<Vælg Fejl Kode>");
        lanejComboBox2.addItem("<Vælg Bane nr>");
        empjComboBox3.addItem("<Vælg Medarbejder>");
        populateComboBoxes();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        weekdayjLabel1 = new javax.swing.JLabel();
        datejLabel2 = new javax.swing.JLabel();
        errorCodejLabel3 = new javax.swing.JLabel();
        errorCodejComboBox1 = new javax.swing.JComboBox();
        laneErrorjLabel4 = new javax.swing.JLabel();
        lanejComboBox2 = new javax.swing.JComboBox();
        empjComboBox3 = new javax.swing.JComboBox();
        empjLabel5 = new javax.swing.JLabel();
        opretjButton1 = new javax.swing.JButton();
        canceljButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        descriptionjLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionjTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        weekdayjLabel1.setText("Ugedag");

        datejLabel2.setText("Dato");

        errorCodejLabel3.setText("Fejl Kode:");

        errorCodejComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                errorCodejComboBox1MouseClicked(evt);
            }
        });
        errorCodejComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                errorCodejComboBox1ActionPerformed(evt);
            }
        });

        laneErrorjLabel4.setText("Bane Nr:");

        empjComboBox3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                empjComboBox3MouseClicked(evt);
            }
        });
        empjComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                empjComboBox3ActionPerformed(evt);
            }
        });

        empjLabel5.setText("Medarbejder:");

        opretjButton1.setText("Opret");
        opretjButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretjButton1ActionPerformed(evt);
            }
        });

        canceljButton2.setText("Annuller");
        canceljButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                canceljButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel1.setText("Opret Bane Fejl");

        descriptionjLabel2.setText("Beskrivelse:");

        descriptionjTextArea1.setColumns(20);
        descriptionjTextArea1.setRows(5);
        jScrollPane1.setViewportView(descriptionjTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(empjLabel5)
                            .addComponent(laneErrorjLabel4)
                            .addComponent(errorCodejLabel3)
                            .addComponent(weekdayjLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(opretjButton1)
                                .addGap(18, 18, 18)
                                .addComponent(canceljButton2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(datejLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                                    .addComponent(errorCodejComboBox1, 0, 114, Short.MAX_VALUE)
                                    .addComponent(lanejComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(empjComboBox3, 0, 163, Short.MAX_VALUE)))))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(descriptionjLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(weekdayjLabel1)
                    .addComponent(datejLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(errorCodejLabel3)
                    .addComponent(errorCodejComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(laneErrorjLabel4)
                    .addComponent(lanejComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(empjLabel5)
                    .addComponent(empjComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(descriptionjLabel2)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(canceljButton2)
                    .addComponent(opretjButton1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void canceljButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_canceljButton2ActionPerformed
        LCP.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_canceljButton2ActionPerformed

private void opretjButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretjButton1ActionPerformed
//    if(errorCodejComboBox1.getSelectedIndex() != 0 && lanejComboBox2.getSelectedIndex() != 0 && descriptionjTextArea1.getSelectedText().length() > 0)
//    {
//    
        LaneError laneErrorObj = new LaneError();
        GregorianCalendar date = new GregorianCalendar();
        date.setTimeInMillis(dateInTable.getTime());
        laneErrorObj.setDate(date);
        
        
        String laneErrorNo = (String)errorCodejComboBox1.getSelectedItem();
        Lane laneObj = (Lane)lanejComboBox2.getSelectedItem();
        Employee empObj = (Employee)empjComboBox3.getSelectedItem();
        String description = descriptionjTextArea1.getText();
        
        laneErrorObj.setErrorCode(laneErrorNo);
        laneErrorObj.setLane(laneObj);
        laneErrorObj.setEmployee(empObj);
        laneErrorObj.setDescription(description);
        
        int rc = laneErrCTR.insertLaneError(laneErrorObj, date);

        
        if(rc == 1)
                {
                    JOptionPane.showMessageDialog(this, "Fejlen er Oprettet", "Oprettelse lykkes",JOptionPane.INFORMATION_MESSAGE);
                    //System.out.println("Fejl Oprettet");
                    //LCP.addLaneErrors(date);
                    LCP.updateTable();
                    LCP.setEnabled(true);
                    
                    this.dispose();
                }        
        else
            {
                JOptionPane.showMessageDialog(this, "Fejl", "Fejl", JOptionPane.ERROR_MESSAGE);
            }
//    }     
//    else
//    {
//        JOptionPane.showMessageDialog(this, "Alt skal udfyldes", "Fejl - Udfyld alle felter", JOptionPane.ERROR_MESSAGE);
//    }
}//GEN-LAST:event_opretjButton1ActionPerformed
    
    private void errorCodejComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_errorCodejComboBox1ActionPerformed
        
    }//GEN-LAST:event_errorCodejComboBox1ActionPerformed

    private void errorCodejComboBox1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_errorCodejComboBox1MouseClicked
        
    }//GEN-LAST:event_errorCodejComboBox1MouseClicked

    private void empjComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_empjComboBox3ActionPerformed

    }//GEN-LAST:event_empjComboBox3ActionPerformed

    private void empjComboBox3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_empjComboBox3MouseClicked
        
    }//GEN-LAST:event_empjComboBox3MouseClicked

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        LCP.setEnabled(true);
    }//GEN-LAST:event_formWindowClosing
    
    private void populateComboBoxes(){
        for (Employee employees : empCTR.getAllEmployees())
        {
            empjComboBox3.addItem(employees);
        }
        for(Lane lanes : laneCTR.getAllLanes())
        {
            lanejComboBox2.addItem(lanes);
        }
        for(LaneErrorDescription laneErrorDescriptions : laneErrCTR.getAllLaneErrorCodes())
        {
            errorCodejComboBox1.addItem(laneErrorDescriptions.getErrorCode());
        }

    }



    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CreateLaneErrorGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton canceljButton2;
    private javax.swing.JLabel datejLabel2;
    private javax.swing.JLabel descriptionjLabel2;
    private javax.swing.JTextArea descriptionjTextArea1;
    private javax.swing.JComboBox empjComboBox3;
    private javax.swing.JLabel empjLabel5;
    private javax.swing.JComboBox errorCodejComboBox1;
    private javax.swing.JLabel errorCodejLabel3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel laneErrorjLabel4;
    private javax.swing.JComboBox lanejComboBox2;
    private javax.swing.JButton opretjButton1;
    private javax.swing.JLabel weekdayjLabel1;
    // End of variables declaration//GEN-END:variables
}
